package ca.seia.comp6521.a1.memory;

import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.List;

import org.hamcrest.core.IsEqual;

import ca.seia.comp6521.a1.io.DataFileReader;
import ca.seia.comp6521.a1.io.FileReadResult;

public class InputMemoryBuffer extends MemoryBuffer {

	DataFileReader reader; 
	int nbrOfIntegersRead;
	
	public InputMemoryBuffer(String assignedSublistFile, int capacity) throws FileNotFoundException {
		super(assignedSublistFile, capacity);
		this.reader = new DataFileReader(this.assignedSublistFile);
	}
	

	public InputMemoryBuffer(int capacity)
	{
		super(capacity);
	}
	
	public InputMemoryBuffer(int capacity, int id) {
		super(capacity, id);
	}
	
	@Override
	public boolean isBufferFull() {
		//System.out.println("isBuffer full for id " + this.id );
		//System.out.println("pointer is at " + this.pointer );
		//System.out.println("numbers of integersReadFrom Last Read is  " + this.nbrOfIntegersRead );
		return super.isBufferFull() || pointer == nbrOfIntegersRead ;
	}


	public FileReadResult load(){
		FileReadResult readResult = this.reader.readNextIntegers(this.capacity, this.buffer);
	
		if (readResult.isEndOfFile()) {
			this.nbrOfIntegersRead = readResult.getNbrIntsRead();
		}
		this.clear();
		return readResult;
	}
	
	public void setAssignedSublistFile(String assignedSublistFile) throws FileNotFoundException {
		this.clear();
		this.nbrOfIntegersRead = 0;
		this.assignedSublistFile = assignedSublistFile;
		this.reader = new DataFileReader(assignedSublistFile);
	}
	
}
